package control;

import java.net.URL;
import java.util.Vector;
import org.apache.soap.Constants;
import org.apache.soap.rpc.Call;
import org.apache.soap.rpc.Parameter;

/**
 * GetDataClient
 * @author Adriano de Albuquerque Batista
 * @author Alfeu Buriti
 * @author Pablo Diego Alencar
 * @author Yuri Cariry Cabral de Melo
 *
 */
public class GetDataClient {


	/**
	 * Create SOAP request to retrieve family and bibliographic data
	 * Publication number in EPOQUE format required as seed
	 * Send request and retrieve response
	 * @param seed seed
	 * @return result
	 */
	public String getFamilyAndBiblio(String seed) {
		String result = "";
		String inputRequest = "" + "" + "" + "" + "";
		try {
			result = sendAndRetrieve(inputRequest);
		} catch (Exception e) {
			System.out
			.println("Error has occurred in sending/retrieving data to OPS web service");
			System.err.println(e);
		}
		return result;
	}


	/**
	 * Create SOAP request to retrieve bibliographic data
	 * Application number in EPOQUE format required as seed
	 * Send request and retrieve response
	 * @param seed seed
	 * @return result
	 */
	public String getBiblio(String seed) {
		String result = "";
		String inputRequest = "" + "" + "" + "" + "" + "";		
		try {
			result = sendAndRetrieve(inputRequest);
		} catch (Exception e) {
			System.out
			.println("Error has occurred in sending/retrieving data to OPS web service");
			System.err.println(e);
		}
		return result;
	}


	/**
	 * Create SOAP request to retrieve bibliographic data
	 * Application number in EPOQUE format required as seed
	 * Send request and retrieve response
	 * @param seed seed
	 * @return result
	 */
	public String getFamily(String seed) {
		String result = "";
		String inputRequest = "" + "" + "" + "" + "" + "";
		try {
			result = sendAndRetrieve(inputRequest);
		} catch (Exception e) {
			System.out
			.println("Error has occurred in sending/retrieving data to OPS web service");
			System.err.println(e);
		}
		return result;
	}




	/**
	 * sendAndRetrieve
	 * @param inputRequest inputRequest
	 * @return result
	 * @throws Exception Exception
	 */
	private String sendAndRetrieve(String inputRequest) throws Exception {
		String result = "";

		// Set SOAP web service properties
		URL url = new URL(
		"http://ops.espacenet.com/OpenPatentServices/webService");
		Call call = new Call();
		call.setTargetObjectURI("urn:EPO-OpenPatentServices");
		call.setMethodName("getPatentData");
		call.setEncodingStyleURI(Constants.NS_URI_LITERAL_XML);

		// Set parameters for input request
		Vector<Parameter> params = new Vector<Parameter>();
		params.addElement((Parameter) new org.apache.soap.rpc.Parameter(
				"input_request", String.class, inputRequest,
				Constants.NS_URI_SOAP_ENC));
		call.setParams(params);
		org.apache.soap.rpc.Response resp;

		try {
			resp = call.invoke(url, ""); 
		} catch (org.apache.soap.SOAPException e) {			
			return (String) e.getMessage();
		}
		// If no response generated, convert response into String and return
		if (!((org.apache.soap.rpc.Response) resp).generatedFault()) {
			org.apache.soap.rpc.Parameter ret = resp.getReturnValue();
			result = ret.getValue().toString();
		}
		return result;
	}


}
